<template>
    <div>
        <el-form ref="CustomerForm" :inline="true" :rules="queryRules" :model="CustomerForm" class="tpl-form-inline pb0">
            <el-form-item label="时间">
                <el-date-picker v-model="CustomerForm.birthday" type="daterange" range-separator="~" format="yyyy 年 MM 月 dd 日" :picker-options="pickerOptions"
                    start-placeholder="开始日期" end-placeholder="结束日期"> </el-date-picker>
            </el-form-item>
            <el-form-item label="省">
                <el-select v-model="CustomerForm.bsType" placeholder="全部">
                </el-select>
            </el-form-item>
            <el-form-item label="市">
                <el-select v-model="CustomerForm.zjType" placeholder="全部">
                </el-select>
            </el-form-item>
            <el-form-item label="区">
                <el-select v-model="CustomerForm.bxType" placeholder="全部">
                </el-select>
            </el-form-item>
            <el-form-item label="灾难类型">
                <el-select v-model="CustomerForm.type" placeholder="全部">
                    <el-option label="暴雨" value="暴雨"></el-option>
                    <el-option label="积水" value="积水"></el-option>
                    <el-option label="暴雪" value="暴雪"></el-option>
                    <el-option label="冰雹" value="冰雹"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="预警车辆数">
                <el-select v-model="CustomerForm.bxType" placeholder="全部">
                </el-select>
            </el-form-item>
            <el-form-item label="避险车辆数">
                <el-select v-model="CustomerForm.bxType" placeholder="全部">
                </el-select>
            </el-form-item>
            <el-form-item label="VIN" prop="secContName">
                <el-input v-model.trim="CustomerForm.vCode" placeholder="请输入"></el-input>
            </el-form-item>
        </el-form>
        <div class="mt10 mb20">
            <el-button type="primary" icon="el-icon-search" @click="queryList" :disabled="loading">查询</el-button>
            <el-button @click="reset" icon="el-icon-refresh" :disabled="loading">重置</el-button>
        </div>

        <div class="text item">
            <el-table class="sd" stripe border :data="tableData" v-loading="loading">
                <el-table-column align="center" prop="index" label="序号" width="100"></el-table-column>
                <el-table-column align="center" prop="name" label="名称"></el-table-column>
                <el-table-column align="center" prop="znDate" label="灾难时间段"></el-table-column>
                <el-table-column align="center" prop="yjDate" label="预警时间段"></el-table-column>
                <el-table-column align="center" prop="type" label="类型"></el-table-column>
                <el-table-column align="center" prop="area" label="地区"></el-table-column>
                <el-table-column align="center" prop="yjNums" label="预警车辆数"></el-table-column>
                <el-table-column align="center" prop="bxNums" label="避险车辆数"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text"
                            @click="handleEdit('detail', scope.row)">查看车辆</el-button>
                        <el-button size="mini" type="text"
                            @click="$router.push({path: '/positionDetail'})">查看位置</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination class="tpl-mg-t mb20 mt20 fr"
                            small
                            @size-change="pageSizeChange"
                            @current-change="pageCurrentChange"
                            :current-page="CustomerForm.pageNum"
                            :page-sizes="[10, 20, 30, 40, 50, 100]"
                            :page-size="CustomerForm.pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total">
            </el-pagination>
        </div>

    </div>
</template>

<script>
    export default {
        name: 'hedgingAreaInfo',
        data () {
            // 表单校验手机号 （0-11位数字）
            let phoneValidator = (rule, value, callback) => {
                if (!value || value === '' || value.match(/^\d{0,11}$/)) {
                    callback()
                } else {
                    callback(new Error('只能输入数字'))
                }
            }
            return {
                // 查询表单
                CustomerForm: {
                    birthday: '',
                    bsType: [],
                    zjType: [],
                    bxType: [],
                    type: '',
                    vCode: ''
                },
                queryRules: {
                    birthday: [
                        { type: 'date', message: '请选择日期', trigger: 'change' }
                    ],
                    bsType: [
                        { message: '请选择保司', trigger: 'change' }
                    ],
                    zjType: [
                        { message: '请选择主机厂', trigger: 'change' }
                    ],
                    bxType: [
                        { message: '请选择保险状态', trigger: 'change' }
                    ],
                    vCode: [
                        {  message: '请输入VIN码', trigger: 'blur' },
                    ]
                },
                // 数据总条数
                // total: 0,
                // 表格数据
                tableData: [],
                // 选中数据
                tableSelection: [],
                pickerOptions: {
                    shortcuts: [{
                        text: '今天',
                        onClick (picker) {
                            const start = new Date()
                            picker.$emit('pick', [start, start])
                        }
                    }]
                },
                params: {},
                loading: false
            }
        },
        mounted () {
            this.queryList()
        },
        computed: {
            total() {
                return this.tableData.length;
            }
        },
        methods: {
            handleEdit (i,r) {
                this.$routerTo('hedgingAreaDetail', { type: i, hedgingAreaData: r, callback: this.getList });
            },
            // 点击查询置为第一页
            queryList () {
                this.CustomerForm.pageNum = 1
                this.params = { ...this.CustomerForm }
                delete this.params.birthday
                for (let key in this.params) {
                    if (!this.params[key] || this.params[key] === '' || this.params[key].length === 0) {
                        delete this.params[key]
                    }
                }
                if (this.CustomerForm.birthday && this.CustomerForm.birthday.length === 2) {
                    this.params.birthdayStart = new Date(new Date(new Date(this.CustomerForm.birthday[0]).setHours(0)).setMinutes(0)).setSeconds(0)
                    this.params.birthdayEnd = new Date(new Date(new Date(this.CustomerForm.birthday[1]).setHours(0)).setMinutes(0)).setSeconds(0) + 24 * 3600 * 1000
                }
                this.getList()
            },
            // 获取列表
            getList () {
                let cData = JSON.parse(localStorage.getItem('hedgingAreaData'));
                if(!cData){
                    cData = [
                        {"index": "1","name": "北京丰台暴雨2号","znDate": "2020-07-21 15:00-17:00","yjDate": "2020-07-21 11:00-14:30","type": "暴雨","area": "北京-丰台区-南京路","yjNums": "253","bxNums": "153"},
                        {"index": "2","name": "北京丰台暴雨2号","znDate": "2020-07-21 15:00-17:00","yjDate": "2020-07-21 11:00-14:30","type": "积水","area": "辽宁-沈阳-和平区-南五","yjNums": "123","bxNums": "23"},
                        {"index": "3","name": "北京丰台暴雨2号","znDate": "2020-07-21 15:00-17:00","yjDate": "2020-07-21 11:00-14:30","type": "冰雹","area": "北京-丰台区-南京路","yjNums": "545","bxNums": "345"},
                        {"index": "4","name": "北京丰台暴雨2号","znDate": "2020-07-21 15:00-17:00","yjDate": "2020-07-21 11:00-14:30","type": "暴雨","area": "辽宁-沈阳-和平区-南五","yjNums": "194","bxNums": "134"},
                        {"index": "5","name": "北京丰台暴雨2号","znDate": "2020-07-21 15:00-17:00","yjDate": "2020-07-21 11:00-14:30","type": "暴雨","area": "北京-丰台区-南京路","yjNums": "253","bxNums": "153"},
                        {"index": "6","name": "北京丰台暴雨2号","znDate": "2020-07-21 15:00-17:00","yjDate": "2020-07-21 11:00-14:30","type": "积水","area": "辽宁-沈阳-和平区-南五","yjNums": "123","bxNums": "23"},
                        {"index": "7","name": "北京丰台暴雨2号","znDate": "2020-07-21 15:00-17:00","yjDate": "2020-07-21 11:00-14:30","type": "冰雹","area": "北京-丰台区-南京路","yjNums": "545","bxNums": "345"},
                        {"index": "8","name": "北京丰台暴雨2号","znDate": "2020-07-21 15:00-17:00","yjDate": "2020-07-21 11:00-14:30","type": "暴雨","area": "辽宁-沈阳-和平区-南五","yjNums": "194","bxNums": "134"}
                    ]
                }
                localStorage.setItem('hedgingAreaData', JSON.stringify(cData));
                this.tableData = cData;
            },
            // 重置表单
            reset () {
                this.CustomerForm = {
                    birthday: '',
                    bsType: [],
                    zjType: [],
                    bxType: [],
                    vCode: '',
                    pageNum: 1,
                    pageSize: 10
                }
                this.$nextTick(() => {
                    this.queryList()
                })
            },
            exportFile () {
                if(this.CustomerForm.birthday[0] || this.CustomerForm.vCode != "" || this.CustomerForm.bsType !== "" || this.CustomerForm.bxType !== "" || this.CustomerForm.zjType !== ""){
                    this.$message({ type: 'success', message: `导出成功!` })
                }else{
                    this.$message.warning('请先选择筛选条件！')
                }
                // let params = { ...this.form }
                // for (let key in params) {
                //     if (params[key] === '' || params[key].length === 0) {
                //         delete params[key]
                //     }
                // }
                // if (this.form.dateRange && this.form.dateRange.length === 2) {
                //     params.tssTimeStart = this.form.dateRange[0]
                //     params.tssTimeEnd = this.form.dateRange[1]
                // } else {
                //     this.$message.warning('请选择发生日期范围')
                //     return
                // }
                // /* 请求部分 */
                // let config = {
                //     headers: {}
                // }
                // let token = window.localStorage.getItem('token') || null
                // let account = window.localStorage.getItem('account') || null
                // if (token) {
                //     config.headers['Token'] = token
                //     config.headers['account'] = account
                //     config.responseType = 'blob'
                // }
                // axios.post(`${baseURL}icall/exportIcall`, params, config).then(res => {
                //     let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
                //     let downloadElement = document.createElement('a')
                //     let href = URL.createObjectURL(blob)
                //     downloadElement.href = href
                //     downloadElement.download = 'chart.xls'
                //     document.body.appendChild(downloadElement)
                //     downloadElement.click()
                //     document.body.removeChild(downloadElement)
                //     window.URL.revokeObjectURL(href)
                // }).catch(e => {
                //     let reader = new FileReader()
                //     reader.onload = event => {
                //         let content = reader.result
                //         let json = JSON.parse(content)
                //         if (json) {
                //             this.$message.error(json.message)
                //         } else {
                //             this.$message.error('导出失败')
                //         }
                //     }
                //     reader.readAsText(e.response.data)
                // })
            },
            // 每页显示条数
            pageSizeChange (val) {
                let pages = Math.ceil(this.total / val)
                this.CustomerForm.pageSize = val
                this.params.pageSize = val
                if (this.CustomerForm.pageNum > pages) {
                    this.CustomerForm.pageNum = 1
                    this.params.pageNum = 1
                }
                this.queryList()
            },
            // 当前页码
            pageCurrentChange (val) {
                this.CustomerForm.pageNum = val
                this.params.pageNum = val
                this.queryList()
            }
        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped rel="stylesheet/scss">

</style>
